<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

use Doraemon\tools\ImgUrl as toolImgUrl;

class OrderOperateLog extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_operate_log';

    //交易类型
    const TRADE_TYPE_ORDER = '1';//订单流程
    const TRADE_TYPE_ORDER_REFUND = '2';//订单退款流程
    const TRADE_TYPE_ORDER_RIGHTS = '3';//订单退款维权的流程


    //状态
    const STATUS_NORMAL = 0;//正常状态
    const STATUS_DELETE = 9;//删除状态

    //操作类型
    const OPERATE_TYPE_CREATE_ORDER = 10;//用户创建订单
    const OPERATE_TYPE_CANCEL_ORDER = 11; //用户取消订单
    const OPERATE_TYPE_AUTO_CANCEL_ORDER = 12; //系统取消订单
    const OPERATE_TYPE_DELETE_ORDER = 13; //用户删除订单
    const OPERATE_TYPE_PROLONG_ORDER = 14; //用户延长收货
    const OPERATE_TYPE_PAY_SUCCESS = 15; //支付成功
    const OPERATE_TYPE_ADMIN_CHANGE_CONSIGNEE_TIME_SUCCESS = 16; //管理员修改用户期望发货时间
    const OPERATE_TYPE_BATCH_ORDER = 19; //商家批量发货
    const OPERATE_TYPE_SHIP_ORDER = 20; //商家确认发货
    const OPERATE_TYPE_CHANGE_ADDRESS = 21; //商家修改收货地址
    const OPERATE_TYPE_CHANGE_SHIP_AMOUNT = 22; //商家变更运费
    const OPERATE_TYPE_CHANGE_EXPRESS_ORDER = 23; //商家跟新物流信息
    const OPERATE_TYPE_USER_CONFIRM_RECEIPT = 33;//用户确认收货
    const OPERATE_TYPE_AUTO_CONFIRM_RECEIPT = 34;//自动确认收货
    const OPERATE_TYPE_APPLY_REFUND_AMOUNT = 50;//申请退款
    const OPERATE_TYPE_APPLY_REFUND_ALL = 51;//申请退货退款
    const OPERATE_TYPE_AGREE_REFUND_APPLY = 52;//同意退款申请
    const OPERATE_TYPE_REJECT_REFUND_AMOUNT = 53;//拒绝退款
    const OPERATE_TYPE_MODIFY_APPLY_REFUND = 54;//修改退款申请
    const OPERATE_TYPE_CANCEL_REFUND_AMOUNT = 55;//取消退款
    const OPERATE_TYPE_CONFIRM_REFUND_AMOUNT = 56;//确认退款
    const OPERATE_TYPE_AGREE_REFUND_GOODS = 57;//同意退货
    const OPERATE_TYPE_REFUNDED_GOODS = 58;//退回商品
    const OPERATE_TYPE_PROLONG_RECEIPT_TIME = 59;//退货延长收货时间
    const OPERATE_TYPE_REFUND_RMB_ERROR = 60;//退款打款失败

    const OPERATE_TYPE_RIGHTS_LAUNCH = 70;//维权发起
    const OPERATE_TYPE_RIGHTS_INTERVENE = 71;//维权介入
    const OPERATE_TYPE_SUBMIT_PROOF = 72;//提交凭证
    const OPERATE_TYPE_JUDGE_RIGHTS = 73;//判定维权
    const OPERATE_TYPE_RIGHTS_CANCEL = 74;//取消维权
    const OPERATE_TYPE_HHZ_CHANGE_ADDRESS = 80;//好好住修改地址
    const OPERATE_TYPE_CLOSE_GROUP_BUY = 81;//退款订单关闭
    const OPERATE_TYPE_EXTEND_REFUND_TIME = 82;//延长订单的可以退款时间

    const OPERATE_TYPE_MODIFY_THE_PARAGRAPH_DESIGNER = 90;//修改节选订单渠道（是设计师订单，还是非设计师订单）


    //操作员类型
    const OPERATE_USERTYPE_USER = 1;//操作员的类型  用户
    const OPERATE_USERTYPE_MGT = 2;//操作员的类型  商家
    const OPERATE_USERTYPE_SYS = 3;//操作员的类型  系统

    public static $rightsTypeDesc = [
        self::OPERATE_TYPE_RIGHTS_LAUNCH => '维权发起',
        self::OPERATE_TYPE_RIGHTS_INTERVENE => '维权介入',
        self::OPERATE_TYPE_SUBMIT_PROOF => '提交凭证',
        self::OPERATE_TYPE_JUDGE_RIGHTS => '判定维权',
    ];

    public static $operateUserTypeDesc = [
        self::OPERATE_USERTYPE_USER => '买家',
        self::OPERATE_USERTYPE_MGT => '商家',
        self::OPERATE_USERTYPE_SYS => '系统'
    ];

    //操作类型描述
    public static $operateTypeDesc = [
        self::OPERATE_TYPE_CREATE_ORDER => '提交订单',
        self::OPERATE_TYPE_USER_CONFIRM_RECEIPT => '用户确认收货',
        self::OPERATE_TYPE_CANCEL_ORDER => '用户取消订单',
        self::OPERATE_TYPE_DELETE_ORDER => '删除订单',
        self::OPERATE_TYPE_PROLONG_ORDER => '延长收货',
        self::OPERATE_TYPE_PAY_SUCCESS => '支付成功',
        self::OPERATE_TYPE_ADMIN_CHANGE_CONSIGNEE_TIME_SUCCESS => '用户期望发货时间',
        self::OPERATE_TYPE_AUTO_CANCEL_ORDER => '系统取消订单',
        self::OPERATE_TYPE_BATCH_ORDER => '批量发货',
        self::OPERATE_TYPE_SHIP_ORDER => '商家确认发货',
        self::OPERATE_TYPE_CHANGE_ADDRESS => '修改地址',
        self::OPERATE_TYPE_CHANGE_SHIP_AMOUNT => '运费变更',
        self::OPERATE_TYPE_CHANGE_EXPRESS_ORDER => '修改物流信息',
        self::OPERATE_TYPE_AUTO_CONFIRM_RECEIPT => '自动确认收货',
        self::OPERATE_TYPE_APPLY_REFUND_AMOUNT => '用户申请退款',
        self::OPERATE_TYPE_APPLY_REFUND_ALL => '用户申请退货退款',
        self::OPERATE_TYPE_AGREE_REFUND_APPLY => '商家同意退款申请',
        self::OPERATE_TYPE_REJECT_REFUND_AMOUNT => '商家拒绝退款',
        self::OPERATE_TYPE_MODIFY_APPLY_REFUND => '修改退款申请',
        self::OPERATE_TYPE_CANCEL_REFUND_AMOUNT => '取消退款',
        self::OPERATE_TYPE_CONFIRM_REFUND_AMOUNT => '商家确认退款',
        self::OPERATE_TYPE_AGREE_REFUND_GOODS => '商家同意退货',
        self::OPERATE_TYPE_REFUNDED_GOODS => '商家退回商品',
        self::OPERATE_TYPE_PROLONG_RECEIPT_TIME => '延长收货时间',
        self::OPERATE_TYPE_RIGHTS_LAUNCH => '维权发起',
        self::OPERATE_TYPE_RIGHTS_INTERVENE => '维权介入',
        self::OPERATE_TYPE_SUBMIT_PROOF => '提交凭证',
        self::OPERATE_TYPE_JUDGE_RIGHTS => '判定维权',
        self::OPERATE_TYPE_RIGHTS_CANCEL => '维权取消',
        self::OPERATE_TYPE_HHZ_CHANGE_ADDRESS => '好好住修改地址',
        self::OPERATE_TYPE_CLOSE_GROUP_BUY => '退款订单关闭',
        self::OPERATE_TYPE_EXTEND_REFUND_TIME => '延长订单的退款时间',

        self::OPERATE_TYPE_MODIFY_THE_PARAGRAPH_DESIGNER => '修改节选订单渠道',

        self::OPERATE_TYPE_REFUND_RMB_ERROR => '退款打款失败',

    ];


    public static function createLog($shopId, $orderNo, $tradeType, $operateData, $refundOrderNo = '', $textContent = '', $imagesContent = '', $upData = [], $addTime = '')
    {
        //插入记录到order_operate_log表
        $data = [
            'shop_id' => $shopId,
            'order_no' => $orderNo,
            'trade_type' => $tradeType,
            'refund_order_no' => $refundOrderNo,
            'text_content' => $textContent,
            'images_content' => $imagesContent,
            'data' => json_encode($upData, JSON_UNESCAPED_UNICODE),
            'addtime' => date('Y-m-d H:i:s')
        ];

        if (!empty($addTime)) {
            $data['addtime'] = $addTime;
        }

        $data = array_merge($data, $operateData);

        $result = self::insert($data);

        return $result;
    }

    public static function getLogList($where, $fields = '*')
    {
        $list = self::select($fields, $where);

        if (empty($list)) {
            return [];
        }

        $list = array_map(function ($v) {
            self::formatInfo($v);

            return $v;
        }, $list);

        return $list;
    }


    public static function formatInfo(&$info)
    {
        if (empty($info)) {
            return;
        }

        foreach ($info as $key => $value) {
            switch ($key) {
                case 'images_content':
                    if (!empty($value)) {
                        $value = json_decode($value, true);
                        $value = array_map(function ($v) {
                            return [
                                'id' => $v,
                                'url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_ORI_NPHONE),
                                'thumb_url' => toolImgUrl::getUrl(toolImgUrl::APPTYPE_BIZ, $v, toolImgUrl::IMAGE_SIZE_SQ_THUMB)
                            ];
                        }, $value);

                    }
                    $info['images_content_url'] = is_array($value) ? $value : [];

                    break;
                case 'operate_type':
                    $info['operate_type_desc'] = self::$operateTypeDesc[$value];
                    break;
                case 'operate_user_type':
                    $info['operate_user_type_desc'] = self::$operateUserTypeDesc[$value];
                    break;

            }
        }
    }
}